package com.lanou.activity.controller;

import com.github.pagehelper.PageInfo;
import com.lanou.activity.Service.LimitService;
import com.lanou.activity.utils.ToolUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/limit")
public class LimitController {

    @Resource
    private LimitService limitService;


    /**
     * @description 跳转到 限额添加页面 addLimit.ftl
     * @author Lonely
     * @date 17/9/23
     * @return
     * @param
     */
    @RequestMapping("findLimit")
    public ModelAndView findLimit() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("addLimit");
        return modelAndView;
    }

    /**
     * @description 添加 限额信息表的数据
     * @author Lonely
     * @date 17/9/23
     * @return
     * @param
     */
    @RequestMapping("addLimitInfo")
    @ResponseBody
    public Map<String,Object> addLimitInfo(@RequestParam Map<String,Object> params) {

//        使用账号生成器自动生成促销账号
        String lmtNo = ToolUtil.AccountGenerator("lmt");
        params.put("lmtNo",lmtNo);

        limitService.addLimitInfo(params);
        params.put("result","success");
        return params;
    }


    /**
     * @param
     * @description 访问限额管理页面
     * @author 66
     * @date 17/9/26
     */
    @RequestMapping("lmt")
    @ResponseBody
    public ModelAndView manageDiscount(@RequestParam Map<String, Object> params) {
        ModelAndView modelAndView = new ModelAndView();
        PageInfo<Map<String, Object>> pageInfo = limitService.selectLmtByPageList(params);
        modelAndView.addObject("params", params);
        modelAndView.addObject("page", pageInfo);
        modelAndView.addObject("list", pageInfo.getList());
        modelAndView.setViewName("manageLmt");
        return modelAndView;
    }

    /**
     * @description  根据不同的限额编码  查询数据回显
     * @author 66
     * @date 17/9/26
     * @param
     */
    @RequestMapping("selectById")
    @ResponseBody
    public  ModelAndView  selectById(@RequestParam("id")Integer id){
        ModelAndView modelAndView=new ModelAndView();
        List<Map<String, Object>> list = limitService.selectLmtById(id);
        Map<String, Object> result=list.get(0);
        result.put("id",id);
        modelAndView.addObject("result",result);
        modelAndView.setViewName("updateLmt");
        return modelAndView;
    }

    /**
     * @description  更新限额信息
     * @author 66
     * @date 17/9/26
     * @param
     */
    @RequestMapping("updateLmt")
    @ResponseBody
    public Map<String,Object>  updateDis(@RequestParam Map<String,Object> params){
        Map map=new HashMap();
        System.out.println(params.toString());
       limitService.updateLmt(params);
        map.put("result","success");
        return  map;
    }



}
